package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.testng.annotations.DataProvider;

import com.example.fw.ContactObject;

public class ContactDataGenerator {
	static Random rnd = new Random();

	@DataProvider(name = "randomContacts")
	public static Iterator<Object[]> generateRandomContacts() {
			List<ContactObject> contacts = new ContactDataGenerator().generateRandomContactList(5);
			List<Object[]> list = new ArrayList<Object[]>();
			for (ContactObject contact: contacts){
				list.add(new Object[]{contact});	}
			return list.iterator();	
	}

	@DataProvider(name = "contactsFromFile")
	public static Iterator<Object[]> loadContactsFromFile() throws IOException {
		List<Object[]> list = new ArrayList<Object[]>();
		BufferedReader reader = new BufferedReader(new FileReader("contacts.dat"));
		String line = reader.readLine();
		while(line != null){
			String[] parts = line.split("\t");
			ContactObject contact = new ContactObject()
			.setFirstname(parts[0])
			.setLastname(parts[1])
			.setAddress(parts[2])
			.setHome(parts[3])
			.setMobile(parts[4])
			.setWork(parts[5])
			.setEmail(parts[6])
			.setEmail2(parts[7])
		//	.setBday("18")
		//	.setBmonth("June")
			.setYear(parts[8])
	//		.setNew_group("Rob")
			.setAddress2(parts[9])
			.setPhone2(parts[10]);
			list.add(new Object[]{contact});
			line = reader.readLine();
		}
		reader.close();
		return list.iterator();	
	}

		public static void main(String[] args){
		if (args.length<2){
			System.out.println("Specify two parameters: file and count");
		return;
		}
			String file = args[0];
			int count = Integer.parseInt(args[1]);
		try {
			new ContactDataGenerator().generateDataToFile(file, count);
		} catch (IOException e) {
			e.printStackTrace();	}
	}

	private void generateDataToFile(String fileName, int count) throws IOException {
		List<ContactObject> contacts = generateRandomContactList(count);
		File file = new File(fileName);
		if (file.exists()){
			//file.delete();
			System.out.println("File exists, stop generator");
		return;
	}
		
		FileWriter writer = new FileWriter(file); 
		for (ContactObject contact : contacts) {
			writer.write(contact.firstname + "\t" + contact.lastname + "\t" +contact.address + "\t" +contact.home + "\t" +contact.mobile + "\t" +contact.work + "\t" +contact.email + "\t" +contact.email2 + "\t" +contact.year + "\t" +contact.address2 + "\t" +contact.phone2 + "\n");
			}
		writer.close();	
	}
		
	private List<ContactObject>  generateRandomContactList(int count){	
	List<ContactObject> list = new ArrayList<ContactObject>();
		for (int i=0; i<count; i++){
			ContactObject contact = new ContactObject()
			.setFirstname(""+rnd.nextInt())
			.setLastname(""+rnd.nextInt())
			.setAddress(""+rnd.nextInt())
			.setHome(""+rnd.nextInt())
			.setMobile(""+rnd.nextInt())
			.setWork(""+rnd.nextInt())
			.setEmail(""+rnd.nextInt())
			.setEmail2(""+rnd.nextInt())
		//	.setBday("18")
		//	.setBmonth("June")
			.setYear(""+rnd.nextInt())
	//		.setNew_group("Rob")
			.setAddress2(""+rnd.nextInt())
			.setPhone2(""+rnd.nextInt());
			list.add(contact);
		}
	return list;
	}	


}
